#include "serverconfig.h"
#include "include/core_server.h"
#include "include/core_log.h"

class TcpServer :public CoreServer
{
public:
	virtual void onClientConnectionMade(CoreClient* pClient)
	{
		CoreLog::getInstance()->info("onClientConnectionMade %d",pClient->getsockfd());
	}
	virtual void onClientConnectionLost(CoreClient* pClient)
	{
		CoreLog::getInstance()->info("onClientConnectionLost %d",pClient->getsockfd());
	}
	virtual void onDataReceived(CoreClient* pClient)
	{
		CoreLog::getInstance()->info("onDataReceived");
	}
	virtual void onPacketReceived(CoreClient* pclient, CoreStream* data)
	{
		int datalen = data->readUInt16();
		int cmd = data->readUInt16();
		int da1 = data->readUInt16();
		int da2 = data->readUInt16();
		CoreLog::getInstance()->info("onPacketReceived %d-%d-%d-%d",datalen,cmd,da1,da2);
	}
};


int main(int argc,const char* argv[])
{
	
	const char* xmlpath = "serverconfig.xml";
	if (argc > 1)
	{
		xmlpath = argv[1];
	}
	ServerConfig::getInstance()->parsexml(xmlpath);
	string ip = ServerConfig::getInstance()->serverip;
	unsigned short port = ServerConfig::getInstance()->port;
	int threadcount = ServerConfig::getInstance()->threadcount;
	CoreLog::getInstance()->setLogPath("server.txt","a+");
	TcpServer tcp;
	tcp.createSocket();
	tcp.bind(ip.c_str(),port);
	tcp.listen(5);
	tcp.start(threadcount);
	while (1)
	{
	}
	return 0;
}